package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.Manifest;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.ActivityPlayLocalBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.TimeUtil;

public class PlayLocalActivity extends BaseAc<ActivityPlayLocalBinding> {

    private AlbumAdapter albumAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_play_local;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivPlayLocalBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mDataBinding.rvLocalList.setLayoutManager(new GridLayoutManager(mContext, 3));
        albumAdapter=new AlbumAdapter();
        mDataBinding.rvLocalList.setAdapter(albumAdapter);
        albumAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PlayActivity.videoUri=albumAdapter.getItem(position).getPath();
        saveRecord(albumAdapter.getItem(position));
        startActivity(PlayActivity.class);
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvLocalList.setVisibility(View.GONE);
                        mDataBinding.tvPlayLocalNoData.setVisibility(View.GONE);
                    }
                }).request();
    }

    private void getData(){
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getApplicationContext(), PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if(selectMediaEntities.size()>0){
                    albumAdapter.setList(selectMediaEntities);
                    mDataBinding.rvLocalList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPlayLocalNoData.setVisibility(View.GONE);
                }
            }
        });
    }

    private void saveRecord(SelectMediaEntity bean) {
        List<RecordBean> listSave = new ArrayList<>();
        String time = TimeUtils.getNowString(new SimpleDateFormat("yyyy/MM/dd"));
        listSave.add(new RecordBean(bean.getMediaName(), TimeUtil.getMmss(bean.getDuration()), ConvertUtils.byte2FitMemorySize(bean.getSize(), 1),
                time, bean.getPath()));
        List<RecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveRecordList(listSp);
        } else {
            AppSpUtils.saveRecordList(listSave);
        }
    }

}